package com.xudu.tourbackend.controller.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xudu.tourbackend.common.DeleteBatchRequest;
import com.xudu.tourbackend.common.PageRequest;
import com.xudu.tourbackend.common.Result;
import com.xudu.tourbackend.common.globalEnum.ErrorCode;
import com.xudu.tourbackend.controller.ScenicSpotController;
import com.xudu.tourbackend.entity.ScenicSpot;
import com.xudu.tourbackend.entity.request.AddScenicSpotRequest;
import com.xudu.tourbackend.entity.request.SearchScenicSpot;
import com.xudu.tourbackend.entity.request.UpdateScenicSpotRequest;
import com.xudu.tourbackend.entity.vo.ScenicAreaVo;
import com.xudu.tourbackend.entity.vo.ScenicSpotVo;
import com.xudu.tourbackend.exception.ServiceException;
import com.xudu.tourbackend.service.IScenicSpotService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * @className: ScenicSpotControllerImpl
 * @description: TODO
 * @author: xudu
 * @create: 2024-06-17
 */
@CrossOrigin(origins = "*")
@RestController
public class ScenicSpotControllerImpl implements ScenicSpotController {

    @Resource
    private IScenicSpotService scenicSpotService;


    @Override
    public Result addScenicSpot(@Valid AddScenicSpotRequest addScenicSpotRequest, HttpServletRequest request) {
        int i = scenicSpotService.addScenicSpot(addScenicSpotRequest, request);
        return Result.success(i);
    }

    @Override
    public Result updateScenicSpot(UpdateScenicSpotRequest updateScenicSpotRequest, HttpServletRequest request) {
        Integer id = updateScenicSpotRequest.getId();
        if (id == null || id <= 0){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        Boolean b = scenicSpotService.updateScenicSpot(updateScenicSpotRequest, request);
        return Result.success(b);
    }

    @Override
    public Result deleteScenicSpot(@RequestBody DeleteBatchRequest deleteBatchRequest, HttpServletRequest request) {
        List<Integer> ids = deleteBatchRequest.getIds();
        if (ids.isEmpty()){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }
        int i = scenicSpotService.deleteScenicSpot(ids, request);
        if (i == 0){
            throw new ServiceException(ErrorCode.SYSTEM_ERROR);
        }
        return Result.success(i);
    }


    @Override
    public Result searchScenicSpot(SearchScenicSpot searchScenicSpot) {
        if (searchScenicSpot == null){
            throw new ServiceException(ErrorCode.PARAMS_ERROR);
        }

        IPage<ScenicSpotVo> scenicSpotVoIPage = scenicSpotService.searchScenicSpot(searchScenicSpot);
        //分页
        //PageRequest pageRequest = new PageRequest();
        //PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
        //PageInfo<ScenicSpotVo> pageInfo = new PageInfo<>(scenicSpotVos);

        return Result.success(scenicSpotVoIPage);
    }


}
